﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QualityStormCommon;

namespace QualityStorm {
    public partial class FrmOPCTagValueTriggerSetup : Form {
        ConnectionContextBag m_contextBag;

        ExternalConnection[] opcConnections;

        public ConnectionContextBag ContextBag {
            get {
                return m_contextBag;
            }
            set {
                m_contextBag = value;
            }
        }
        
        public FrmOPCTagValueTriggerSetup() {
            InitializeComponent();
        }

        public FrmOPCTagValueTriggerSetup(ConnectionContextBag bag)     {
            m_contextBag = bag;
            InitializeComponent();
        }


        private void btnSearch_Click(object sender, EventArgs e) {
            if (cboConnections.SelectedIndex > -1) {
                OPCBranch topBranch = m_contextBag.Context.Service.BrowseOPCServer(opcConnections[cboConnections.SelectedIndex]);
                FrmOPCTagSelection frm = new FrmOPCTagSelection();
                frm.TopOPCBranch = topBranch;
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK) {
                    txtOPCTagName.Text = frm.SelectedTagName;
                }

            }

        }

        private void FrmTriggerSetup_Load(object sender, EventArgs e) {
            opcConnections = m_contextBag.Context.Service.GetExternalConnections(Guid.Empty).Where(ec => ec.Type == 1).ToArray<ExternalConnection>();
            foreach (ExternalConnection opcConnection in opcConnections) {
                cboConnections.Items.Add(opcConnection.Name);
            }





        }

        private void cboConnections_SelectedIndexChanged(object sender, EventArgs e) {
            btnSearch.Enabled = (cboConnections.SelectedIndex > -1);
        }
    }
}
